'''
Created on 16/10/2009

@author: eh01
'''
from ConfigParser import RawConfigParser
from Queue import Queue
from datastore import database
from datastore.database import DataStore
from runtime import rt
from runtime.spaces import Spaces, SpacesContext
from spear.speardsupdate import SpearDSUpdate
import os
import unittest


class SpearDSUpdateTest(unittest.TestCase):


    def setUp(self):
        queue = Queue()
        self.spaces = Spaces()
        spaces_context = SpacesContext(self.spaces, queue)
        runtime = rt.Runtime()
        runtime.initialise_spaces_with_local_config_files(spaces_context)       
        self.temp_database_name = 'test.db'
        self.original_database_name = database.DATABASE_NAME
        database.DATABASE_NAME = self.temp_database_name

    def tearDown(self):
        database.DATABASE_NAME = self.original_database_name
        try:
            os.remove(self.temp_database_name)
        except:
            pass

    def test_update(self):
        '''[speardsupdate] test_update'''
        section = 'a2a data'
        cp = RawConfigParser()
        # the code coverage test does some magic when executing. And the properties_file_path has to be change to use the one below
        # which only just specifies the file name
#        properties_file_path = 'a2a.properties'
        properties_file_path = os.path.dirname(__file__) + '/a2a.properties'
        cp.read(properties_file_path)
        client_cert_file_name = cp.get(section, 'clientcertfilename')
        client_cert_password = cp.get(section, 'clientcertpassword')
        spear_user_username = cp.get(section, 'spearuserusername')
        spear_user_password = cp.get(section, 'spearuserpassword')
        spear_user_remote_user = cp.get(section, 'spearuserremoteuser')
        spear_user_access_level = cp.get(section, 'spearuseraccesslevel')
        uri_datastore = self.spaces.uri('datastore:')
        uri_spear = self.spaces.uri('spear:')
        properties_file_path = os.path.dirname(__file__) + '/a2a.properties'
        spear_ds_update = SpearDSUpdate(database.DATABASE_NAME, properties_file_path)
        spear_ds_update.update()
        ds = DataStore()
        # check for access token
        access_token_resources = ds.find([(uri_datastore.resource.type, uri_spear.type.pvtaccesstoken)])
        self.assertEqual(len(access_token_resources), 1)
        self.assertEqual(access_token_resources[0].spear_user_username, spear_user_username)
        self.assertEqual(access_token_resources[0].spear_user_password, spear_user_password)
        self.assertEqual(access_token_resources[0].spear_user_remote_user, spear_user_remote_user)
        self.assertEqual(access_token_resources[0].spear_user_access_level, spear_user_access_level)
        # check for client cert
        client_certs = ds.find([(uri_datastore.resource.type, uri_spear.type.client_cert), (uri_spear.cert.password, client_cert_password)])
        self.assertEqual(len(client_certs), 1)
        self.assertEqual(client_certs[0].content, open(client_cert_file_name, 'rb').read())
        # check for WSDL
        wsdls = ds.find([(uri_datastore.resource.type, uri_spear.type.wsdl), (uri_spear.wsdl.name, 'LxSpearEcho')])
        self.assertEqual(len(wsdls), 1)
        wsdls = ds.find([(uri_datastore.resource.type, uri_spear.type.wsdl), (uri_spear.wsdl.name, 'LxSpearApplicationList')])
        self.assertEqual(len(wsdls), 1)
        wsdls = ds.find([(uri_datastore.resource.type, uri_spear.type.wsdl), (uri_spear.wsdl.name, 'LxSpearDocumentTypes')])
        self.assertEqual(len(wsdls), 1)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()